package br.usp.icmc.client.controladores;

import java.util.Iterator;
import java.util.List;

import br.usp.icmc.client.dao.ClientDAO;
import br.usp.icmc.client.dao.ServiceDAO;
import br.usp.icmc.client.entidades.Client;
import br.usp.icmc.client.entidades.Service;
import br.usp.icmc.client.exception.RegisterException;

/***************************************************************************
 * <b> <br> Projeto: Aplicativo Cliente <br><b>
 * 
 * Classe de controle do Cliente
 * 
 * @author felipe
 *
 ***************************************************************************/
public class ControllerClient {

	ClientDAO clientDAO;
	Client client;
	Service service;
	ServiceDAO serviceDAO;
	ControllerService controllerService;
	List services;

	
	
	/********************************************************************************
	 * Construtor padrão da entidade ControllerAdmin 
	 ********************************************************************************/
	public ControllerClient(){
		clientDAO = new ClientDAO();
		client = new Client();
		service = new Service();
		controllerService = new ControllerService();
		serviceDAO = new ServiceDAO();
	}
	

	/********************************************************
	 * Remove o cliente do sistema
	 * 
	 * @param email
	 * 				Email do Cliente a ser removido
	 *******************************************************/
	public void removeClient(String email){		
		clientDAO.deleteById(email);		
	}
	
	
	/*******************************************************
	 * Edita a senha do Cliente
	 * 
	 * @param email
	 * 				Email do cliente a ser editado
	 * @param password
	 * 				Nova senha
	 ******************************************************/
	
	public void editClient(String email, String userName, String password){
		client = clientDAO.load(email);	
		client.setPassword(password);
		client.setUserName(userName);
		
		clientDAO.saveOrUpdate(client);
		
	}
	
	
	
	/***************************************************
	 * Adiciona um cliente ao sistema
	 * 
	 * 
	 * @param email
	 * 				Email de registro do cliente
	 * @param password
	 * 				Senha do cliente
	 * @param fullName
	 * 				Nome completo do Cliente
	 * 
	 * @throws RegisterException
	 ***************************************************/
	public void addClient(String email, String userName, String password, String fullName) throws RegisterException{
	
		client.setEmail(email);
		client.setUserName(userName);
		client.setPassword(password);
		client.setFullName(fullName);
		
		
		clientDAO.save(client);
	}
	
	
	/**
	 *  Realiza o contrato de um serviço
	 * 
	 * @param email
	 * 				Email do Cliente 		
	 * @param idService
	 * 				Id do serviço a ser contratado
	 */
	public void contractService(String email, Service serviceContract){
		
		client = clientDAO.load(email);
		client.getContractedServices().add(serviceContract);
		//service.getContractClient().add(client);
		clientDAO.saveOrUpdate(client);
		//serviceDAO.saveOrUpdate(service);
			
	}
	
	public Client associaClient(String email){
		client = clientDAO.load(email);
		return client;
		
	}
	
}
